package com.redteam.models;

import com.redteam.data.beans.Airplane;
import com.redteam.data.beans.Person;
import com.redteam.data.beans.Reservation;
import com.redteam.util.DateConverter;
import com.redteam.util.TimeCalculator;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A model that gets the information needed for the report views
 * @author Teddy Karam
 */
public class Report {

    private DAOAccessor dao;

    /**
     * Default constructor
     */
    public Report() {
        dao = new DAOAccessor();
    }

    /**
     * Returns an array list of all reservations for a given item within a time frame
     * @param rentalItemId
     * @param startDate
     * @param endDate
     * @return reservationByDate
     */
    public List<Reservation> getReservationListSingleItem(long rentalItemId, Date startDate, Date endDate) {
        List<Reservation> reservationByDate = new ArrayList<Reservation>();
        for (Reservation r : dao.getAllforAnyItem(rentalItemId)) {

            if ((startDate.compareTo(r.getStartDate()) <= 0) && (endDate.compareTo(r.getEndDate()) >= 0)) {
                reservationByDate.add(r);
            }
        }
        return reservationByDate;
    }

    /**
     * Returns an array list of all the reservations for a given time frame
     * @param startDate
     * @param endDate
     * @return allReservationsByDate
     */
    public List<Reservation> getReservationListAllItems(Date startDate, Date endDate) {
        List<Reservation> allReservationsByDate = new ArrayList<Reservation>();
        for (Reservation r : dao.getAllReservations()) {
            if (startDate.compareTo(r.getStartDate()) >= 0 & endDate.compareTo(r.getEndDate()) <= 0) {
                allReservationsByDate.add(r);
            }
        }

        return allReservationsByDate;
    }

    /**
     * Returns an array list of all the reservation for given person
     * @param person
     * @return reservationListPerPerson
     */
    public List<Reservation> getReservationListPerPerson(Person person) {

        return dao.getReservationListPerPersonID(person.getId());
    }

    /**
     * Returns a 2 dimension array of string containing the data needed for the Aircraft/Simulator rental history detail.
     * @param rentalItemId
     * @param startDate
     * @param endDate
     * @return historyDetail
     */
    public String[][] getAircraftRentalHistoryDetail(long rentalItemId, Date startDate, Date endDate) {
        List<Reservation> reservationList = new ArrayList<Reservation>();
        reservationList = getReservationListSingleItem(rentalItemId, startDate, endDate);
        String[][] historyDetail = new String[reservationList.size()][10];
        int row = 0;
        for (Reservation r : reservationList) {
            historyDetail[row][0] = Long.toString(r.getId());

            historyDetail[row][1] = r.getDiscriminator();

            historyDetail[row][2] = r.getRentalItem().getAircraftType().getAircraftType();
            historyDetail[row][3] = r.getReserver().getFirstName() + " " + r.getReserver().getLastName();
           
            historyDetail[row][4] = r.getPilot().getFirstName() + " " + r.getPilot().getLastName();



            historyDetail[row][5] = DateConverter.convertDateSqlToRegular(r.getStartDate().toString());

            historyDetail[row][6] = DateConverter.convertDateSqlToRegular(r.getEndDate().toString());

            historyDetail[row][7] = (new TimeCalculator(r.getStartDate(), r.getEndDate(), r.getStartTime(), r.getEndTime())).toString();


            historyDetail[row][8] = r.getAgreement();
            historyDetail[row][9] = Double.toString(r.getPrice());
            row++;


        }

        return historyDetail;
    }

    /**
     *Returns a 2 dimensions array containing all the rentals for a given time frame.
     * @param startDate
     * @param endDate
     * @return historySummary
     */
    public String[][] getAllRentalHistorySummary(Date startDate, Date endDate) {
        List<Reservation> reservationList = new ArrayList<Reservation>();
        reservationList = getReservationListAllItems(startDate, endDate);
        String[][] historySummary = new String[reservationList.size()][10];
        int row = 0;
        for (Reservation r : reservationList) {
            historySummary[row][0] = Long.toString(r.getId());
            historySummary[row][1] = r.getRentalItem().getType();
            historySummary[row][2] = r.getRentalItem().getAircraftType().getAircraftType();
            historySummary[row][3] = DateConverter.convertDateSqlToRegular(r.getStartDate().toString());
            historySummary[row][4] = DateConverter.convertDateSqlToRegular(r.getEndDate().toString());
            historySummary[row][5] = r.getStartTime().toString();
            historySummary[row][6] = r.getEndTime().toString();
            historySummary[row][7] = (new TimeCalculator(r.getStartDate(), r.getEndDate(), r.getStartTime(), r.getEndTime())).toString();
            historySummary[row][8] = Long.toString(r.getReserver().getId());
            historySummary[row][9] = Double.toString(r.getPrice());
            row++;




        }
        return historySummary;
    }

    /**
     * Returns a 2 dimensions array containing all the rentals for a specific renter.
     * @param personID
     * @return renterReport
     */
    public String[][] getRenterReport(long personID) {
        List<Reservation> reservationList = new ArrayList<Reservation>();
        reservationList = dao.getReservationListPerPersonID(personID);
        String[][] renterReport = new String[reservationList.size()][10];
        int row = 0;
        for (Reservation r : reservationList) {
            renterReport[row][0] = Long.toString(r.getId());
            renterReport[row][1] = r.getRentalItem().getType();
            renterReport[row][2] = r.getRentalItem().getAircraftType().getAircraftType();

            if (r.getRentalItem().getType().equals("airplane")) {
                Airplane plane = null;
                try {
                    plane = dao.getPlane(r.getRentalItem().getId());
                } catch (SQLException ex) {
                    Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
                }
                renterReport[row][3] = plane.getTailNumber();
            } else {
                renterReport[row][3] = "N/A";
            }

            renterReport[row][4] = DateConverter.convertDateSqlToRegular(r.getStartDate().toString());
            renterReport[row][5] = DateConverter.convertDateSqlToRegular(r.getEndDate().toString());
            renterReport[row][6] = r.getStartTime().toString();
            renterReport[row][7] = r.getEndTime().toString();
            renterReport[row][8] = (new TimeCalculator(r.getStartDate(), r.getEndDate(), r.getStartTime(), r.getEndTime())).toString();

            renterReport[row][9] = r.getPilot().getFirstName() + " " + r.getPilot().getLastName();
            row++;


        }

        return renterReport;
    }

    /**
     * Returns a string array containing all the informations needed to generate an airplane status report
     * @param airplaneID
     * @return statusReport
     */
    public String[] AircraftStatusReport(long airplaneID) {

        Airplane airplane = new Airplane();
        try {
            airplane = dao.getPlane(airplaneID);
        } catch (SQLException ex) {
            Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
        }
        String[] statusReport = new String[10];
        statusReport[0] = Long.toString(airplane.getId());
        statusReport[1] = airplane.getTailNumber();
        statusReport[2] = airplane.getModel();
        statusReport[3] = airplane.getAircraftType().getAircraftType();
        statusReport[4] = Double.toString(airplane.getHobbsLastHundred());
        statusReport[5] = Double.toString(airplane.getHobbsLastOverhaul());
        statusReport[6] = Double.toString(airplane.getHobbsLeftEngine());
        statusReport[7] = Integer.toString(airplane.getLandings());
        statusReport[8] = Integer.toString(airplane.getTakeOffs());

        return statusReport;
    }

    /**
     * Returns a string array containing all the informations needed to generate an airplane status report
     * @param airplane
     * @return statusReport
     */
    public String[] AircraftStatusReport(Airplane airplane) {

        return AircraftStatusReport(airplane.getId());
    }
}
